Search this book | Previous | Table of contents | Next

Global variables in .script scripts


This section demonstrates how .script files are modified before execution to include a number of standard variables.

After MacHTTP loads a .script file into RAM and before it sends the script to AppleScript for compilation and execution, MacHTTP prepends to your script a number of predefined global variables. These variables are pieces of information sent from many (but not all) client applications and include:

path_args
The data after the $ and before a ? in a URL
http_search_args
All data after the ? of a URL
method
Either GET or POST (.script files always return GET)
post_args
(Always empty because the method is never POST)
client_address
The IP address or name of the client's host computer
username
The validated person accessing the script
password
The validated password of the person accessing the script
from_user
The name of the person (from the client's preferences) accessing the script
server_name
The IP address or name of the computer running the server
server_port
The port the server is running on (most likely 80)
script_name
The name of the script to being executed
content_type
The MIME type
referer
The URL of the page containing the script
user_agent
The name of the client application
Because MacHTTP prepends these variables and their values to every .script file sent to AppleScript, you can read and manipulate these values in that same .script file as shown below in hello-world-03.script:
-- define the standard HTTP header
set LF to ASCII character (10)
set CR to return
set CRLF to CR & LF
set http_10_header to "HTTP/1.0 200 OK" & CRLF & ¬
	"Server: MacHTTP" & CRLF & ¬
	"MIME-Version: 1.0" & CRLF & ¬
	"Content-type: text/html" & CRLF & CRLF

-- return the results, including the prepended variables, as an HTML file
return http_10_header & ¬
	"<html>" & ¬
	"<head>" & ¬
	"<title>Hello, World</title>" & ¬
	"</head>" & ¬
	"<body>Hello, World!" & ¬
	"<p>http_search_args: " & http_search_args & ¬
	"<br>path_args: " & path_args & ¬
	"<br>post_args: " & post_args & ¬
	"<br>method: " & method & ¬
	"<br>client_address: " & client_address & ¬
	"<br>username:" & username & ¬
	"<br>password: " & password & ¬
	"<br>from_user:" & from_user & ¬
	"<br>server_name:" & server_name & ¬
	"<br>server_port:" & server_port & ¬
	"<br>script_name: " & script_name & ¬
	"<br>content_type: " & content_type & ¬
	"<br>referer: " & referer & ¬
	"<br>user_agent: " & user_agent & ¬
	"</body>" & ¬
	"</html>"

Consequently, based on these global variables, you can process your script differently. For example, you may want to test whether or not the client application understands "Netscapisms" by examining the contents of user_agent. If the user_agent can render "Netscapisms", then you might return one type of HTML mark up. If it doesn't, then you might return a different markup.


Search this book | Previous | Table of contents | Next

Eric last edited this page on September 26, 1995. Please feel free to send comments.